<?php

namespace App\Models;

use App\Models\Contract\SerializeDatetime;
use Illuminate\Database\Eloquent\Model;

class Transaction extends Model
{
    use SerializeDatetime;

    protected $fillable = [
        'user_id', 'user_withdraw_id', 'transaction_no', 'trans_amount', 'batch_detail_no',
        'account_type', 'account_no', 'remote_order_no', 'bankinfo', 'status', 'package_info'
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function withdraw()
    {
        return $this->belongsTo(UserWithdraw::class);
    }

    public static function generateSerialNumber()
    {
        $rand = substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 12);
        $randomString = date('Ymd') . $rand;

        if (self::query()->where('transaction_no', $randomString)->exists()) {
            return self::generateSerialNumber();
        }

        return $randomString;
    }

    public static function generateDetailSerialNumber()
    {
        return 'detail001';
    }
}
